package logic

import (
	"MyBlogs/dao/mysql"
	"MyBlogs/models"
	"MyBlogs/tools"
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"
	"time"
)

// 判断用户名 是否重复
func FindOneUser(context *gin.Context) {
	a, _ := context.GetPostForm("uname")
	b := mysql.FindOneUser(a)
	if !b {
		context.JSON(200, gin.H{
			"mag": "该账户已存在",
		})
	} else {
		context.JSON(200, gin.H{
			"mag": "",
		})
	}
}

// 注册成功后 添加
func AddUser(context *gin.Context) {
	var user *models.User
	err := context.ShouldBind(&user)
	if err != nil {
		fmt.Printf(err.Error())
	}
	if !strings.Contains(user.Name, " ") || !strings.Contains(user.Pwd, " ") || !strings.Contains(user.Email, " ") {
		user.CreateTime = time.Now().Format("2006-01-02 15:04:05")
		user.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
		user.State = "正常"
		user.Uid = tools.UID()
		mysql.AddUser(user)
		context.JSON(200, gin.H{
			"data": "注册成功！",
		})

	} else {
		context.JSON(200, gin.H{
			"data": "账号或密码中含有非法字符！",
		})
	}

}
